import sys

# 直接复用Pipeline实例
sys.path.append("./")
pipeline = __import__("06-fill-mask")

import gradio as gr


# 定义填充蒙版函数
def fill_mask(sentence):
    result = pipeline.nlp(sentence)
    return "\n".join([f"答案: {r['token_str']} (得分: {r['score']:.2f})" for r in result])

# 创建Gradio界面
with gr.Blocks() as demo:
    gr.Markdown("# 填充蒙版系统")
    gr.Markdown(
        "这是一个基于Transformers框架的填充蒙版工具。您可以输入一句含有[MASK]标记的句子，点击“提交”按钮后，系统将尝试预测最适合填入该位置的词汇。")

    with gr.Row():
        input_sentence = gr.Textbox(placeholder="请输入含有[MASK]标记的句子...", label="句子")

    with gr.Row():
        submit_button = gr.Button("提交")

    with gr.Row():
        output_answer = gr.Label(label="可能的答案")

    # 设置按钮点击事件，触发填充蒙版函数
    submit_button.click(fill_mask, inputs=[input_sentence], outputs=output_answer)

# 启动Gradio应用
if __name__ == "__main__":
    demo.launch()
